using System;
using System.Collections.Generic;
using Mall.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Mall.Api.ParamModel;
using Newtonsoft.Json;
using Mall.Api.Repository;
using Mall.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Mall.Api.Filters;

namespace Mall.Api.Controllers
{
    /// <summary>
    /// 支付管理
    /// </summary>


    // [Authorize]

    [ApiController]
    [Route("payment")]
    public class PaymentController : ControllerBase
    {
        private IRepository<Payment> _payment;

        private readonly IConfiguration _configuration;

        public PaymentController(IConfiguration configuration, IRepository<Payment> payment)
        {
            _configuration = configuration;
            _payment = payment;
        }

        /// <summary>
        /// 获取支付列表
        /// </summary>
        /// 
        /// <returns></returns>
        [HttpGet]
        public dynamic Get()
        {

            // 获得所有满足条件的记录
            var payments = _payment.Table.Where(x => x.IsDeleted == false);


            // 返回的数据里，带有数据和分页信息
            var res = new
            {
                Code = 1000,
                Data = payments,
                Msg = "获取供应商列表成功^_^"
            };
            return res;
        }

        /// <summary>
        /// 修改支付方式的状态
        /// </summary>
        /// <returns></returns>

        [HttpPut("amendactive/{id}")]

        public string AmendPayMentActive(int id, NewPayment model)
        {

            var amendactive = _payment.Table.Where(a => a.Id == id).FirstOrDefault();

           amendactive.IsActived= model.IsActived;
            _payment.Update(amendactive);


             return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = amendactive,
                    Msg = "修改状态成功！"
                });


        }

        /// <summary>
        /// 真删除指定支付方式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var classes = _payment.GetById(id);
            if (classes != null)
            {
                _payment.Delete(id);
                return new
                {
                    Code = 200,
                    Msg = string.Format("删除指定id为{0}的支付方式成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id的支付方式不存在，请确认后重试"
                };
            }

        }




        /// <summary>
        /// 添加支付方式
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public string Post(NewPayment model)
        {

            var paymentName = model.PaymentName.Trim();

            // var rolesid = model.RolesId;
            

            if (string.IsNullOrEmpty(paymentName))
            {
                var tmp = new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "用户名不能为空，请确认后重试"
                };
                return JsonHelper.Serialize(tmp);
            }


            var payments = new Payment
            {

                PaymentName = paymentName

            };

            _payment.Insert(payments);


            var res = new
            {
                Code = 200,
                Data = payments,
                Msg = "添加支付方式成功"
            };
            return JsonHelper.Serialize(res);
        }





    }

}


